package server;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 
 * @author Jesse Gorter
 *
 */
public class LoginImpl implements Login
{
	
	static Remote remote;
	static Registry registry;
	private int port = 1097;
	
	public LoginImpl() throws Exception
	{
		System.setProperty("java.rmi.server.logCalls", "true");
		if (registry == null){
			registry = LocateRegistry.createRegistry(1099);
		}
	}
	public boolean check(String username, String password)
	{
		DatabaseImpl db = new DatabaseImpl();
		ResultSet rs = db.executeQuery("SELECT username FROM users WHERE username = '" + username + "' AND password = '" + password + "'");
		try {
			if (rs.next()){
				return true;
			}
		}catch (SQLException sqle){}
		return false;
	}
	/**
	 * Checkt de gebruikersnaam en wachtwoord en geeft een loginObject terug als deze niet kloppen.
	 */
	public Cijferlijst login(String username, String password) throws RemoteException 
	{
		if(check(username, password))
		{	
			
			
			if(remote == null)
			{
				CijferlijstImpl cijfer = new CijferlijstImpl();
				remote = UnicastRemoteObject.exportObject(cijfer, port);
				return (Cijferlijst)remote;
			}
			else
			{	
				
				return (Cijferlijst)remote;
			}
			
		}
		else
		{
			return null;
		}
		
	}

}
